package br.com.projeto.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import br.com.projeto.model.Jogo;
import br.com.projeto.task.AtualizarJogoTask;
import br.com.projeto.task.ExcluirJogoTask;

import com.tbm.megasena.R;

public class ListarJogosAdapter extends BaseAdapter {

	private List<Jogo> games;
	private ViewHolder holder;
	private LayoutInflater layoutInflater;
	private Context context;
	private Jogo jogo;
	
	private DialogInterface.OnClickListener modalOnChangeGame;
	
	public ListarJogosAdapter(final Context context, List<Jogo> games, final Handler handler) {
		this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.games = games;
		this.context = context;
		
		modalOnChangeGame = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_POSITIVE:
					new AtualizarJogoTask(handler).execute(jogo);
					break;

				case DialogInterface.BUTTON_NEGATIVE:
					// showProgressDialog(context.getString(R.string.saving));
					new ExcluirJogoTask(handler).execute(jogo);
					break;
				}
			}
		};
	}

	private class ViewHolder {
		private TextView txtNumber1;
		private TextView txtNumber2;
		private TextView txtNumber3;
		private TextView txtNumber4;
		private TextView txtNumber5;
		private TextView txtNumber6;
		private TextView txtNumberOfHit;
		private LinearLayout lnItemList;
	}
	
	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		if (view == null) {
			view = layoutInflater.inflate(R.layout.ver_resultados_item, null);
			holder = new ViewHolder();

			holder.txtNumber1 = (TextView) view.findViewById(R.id.txtNumber1);
			holder.txtNumber2 = (TextView) view.findViewById(R.id.txtNumber2);
			holder.txtNumber3 = (TextView) view.findViewById(R.id.txtNumber3);
			holder.txtNumber4 = (TextView) view.findViewById(R.id.txtNumber4);
			holder.txtNumber5 = (TextView) view.findViewById(R.id.txtNumber5);
			holder.txtNumber6 = (TextView) view.findViewById(R.id.txtNumber6);
			holder.txtNumberOfHit = (TextView) view.findViewById(R.id.txtNumberOfHit);
			
			holder.lnItemList = (LinearLayout) view.findViewById(R.id.lnItemList);
			
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		
		Jogo game = games.get(position);
		
		holder.txtNumber1.setText(game.getNumero1());
		holder.txtNumber2.setText(game.getNumero2());
		holder.txtNumber3.setText(game.getNumero3());
		holder.txtNumber4.setText(game.getNumero4());
		holder.txtNumber5.setText(game.getNumero5());
		holder.txtNumber6.setText(game.getNumero6());
		
		holder.txtNumberOfHit.setText("?");
		
		if (context.getClass().getName().equals("br.com.projeto.activity.SavarJogoActivity")) {		
			holder.lnItemList.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					jogo = games.get(position);
					
					new AlertDialog.Builder(context).setMessage(context.getText(R.string.escolha)) //
					.setPositiveButton(context.getText(R.string.atualizar), modalOnChangeGame) // 
					.setNegativeButton(context.getText(R.string.excluir), modalOnChangeGame).setCancelable(false).show();
				}
			});
		}
		return view;
	}

	@Override
	public int getCount() {
		return games.size();
	}

	@Override
	public Object getItem(int position) {
		return games.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}
